﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using net.veekee.common;
using net.veekee.common.data.csv;
using System.Collections;
using net.veekee.logprocessor.plugin.output;

namespace net.veekee.logprocessor.plugin.output.pack
{
    class PlainWriter : AWriter
    {
        private StreamWriter txt = null;

        public override bool Load(String filename)
        {
            try
            {
                this.FileName = filename;
                this.txt = new StreamWriter(this.FileName, this.context.Append, Encoding.GetEncoding(this.context.Encoding));
                return true;
            }
            catch (Exception e)
            {
                Log.GetInstance().Error(e.ToString());
            }
            return false;
        }

        protected override void AddLine(IEnumerator items)
        {
            if (this.txt == null)
                return;
            StringBuilder sb = new StringBuilder();
            while (items.MoveNext())
            {
                sb.Append(items.Current.ToString());
            }
            this.txt.WriteLine(sb.ToString());
            this.txt.Flush();
        }

        protected override void WriteHeader()
        {
            if (this.txt == null)
                return;
        }

        public override void WriteFooter()
        {
            if (this.txt == null)
                return;
        }

        public override void Dispose()
        {
            if (this.txt == null)
                return;
            this.WriteFooter();
            this.txt.Close();
            this.txt.Dispose();
        }
    }
}
